package com.ithiem.dubbo.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dubbo.api.AnnouncementsApi;
import com.itheima.model.domain.Announcement;
import com.itheima.model.vo.AnnouncementVo;
import com.ithiem.dubbo.mappers.AnnouncementsMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class AnnouncementsApiImpl implements AnnouncementsApi {

    @Autowired
    private AnnouncementsMapper announcementsMapper;

    // 分页查询公告列表
    @Override
    public IPage<AnnouncementVo> getAnnouncements(Integer page, Integer pagesize) {
        // 构造分页参数page
        Page pages = new Page(page,pagesize);
        // 调用AnnouncementsMapper进行分页查询
        IPage<Announcement> announcementIPage = announcementsMapper.getAnnouncements(pages);
        // 将列表内的Announcement对象转为AnnouncementVo对象
        IPage<AnnouncementVo> announcementVoIPage = new Page<>();

        BeanUtils.copyProperties(announcementIPage,announcementVoIPage,"records");

        List<Announcement> announcementList = announcementIPage.getRecords();
        List<AnnouncementVo> announcementVos = new ArrayList<>();
        for (Announcement announcement : announcementList) {
            AnnouncementVo announcementVo = AnnouncementVo.init(announcement);
            announcementVos.add(announcementVo);
        }
        announcementVoIPage.setRecords(announcementVos);

        return announcementVoIPage;
    }
}
